package org.wu.framework.ssh.server.platform.infrastructure.persistence;

import org.wu.framework.ssh.server.platform.infrastructure.entity.SshServerConfigDO;
import org.wu.framework.ssh.server.platform.infrastructure.converter.SshServerConfigConverter;
import org.wu.framework.ssh.server.platform.domain.model.ssh.server.config.SshServerConfigRepository;
import org.springframework.stereotype.Repository;

import java.util.stream.Collectors;

import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import org.wu.framework.ssh.server.platform.domain.model.ssh.server.config.SshServerConfig;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;

/**
 * describe ssh服务器信息
 *
 * @author Jia wei Wu
 * @date 2025/09/13 10:07 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class SshServerConfigRepositoryImpl implements SshServerConfigRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增ssh服务器信息
     *
     * @param sshServerConfig 新增ssh服务器信息
     * @return {@link Result<SshServerConfig>} ssh服务器信息新增后领域对象
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Override
    public Result<SshServerConfig> story(SshServerConfig sshServerConfig) {
        SshServerConfigDO sshServerConfigDO = SshServerConfigConverter.INSTANCE.fromSshServerConfig(sshServerConfig);
        lazyLambdaStream.upsertRemoveNull(sshServerConfigDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增ssh服务器信息
     *
     * @param sshServerConfigList 批量新增ssh服务器信息
     * @return {@link Result<List<SshServerConfig>>} ssh服务器信息新增后领域对象集合
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Override
    public Result<List<SshServerConfig>> batchStory(List<SshServerConfig> sshServerConfigList) {
        List<SshServerConfigDO> sshServerConfigDOList = sshServerConfigList.stream().map(SshServerConfigConverter.INSTANCE::fromSshServerConfig).collect(Collectors.toList());
        lazyLambdaStream.upsert(sshServerConfigDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个ssh服务器信息
     *
     * @param sshServerConfig 查询单个ssh服务器信息
     * @return {@link Result<SshServerConfig>} ssh服务器信息领域对象
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Override
    public Result<SshServerConfig> findOne(SshServerConfig sshServerConfig) {
        SshServerConfigDO sshServerConfigDO = SshServerConfigConverter.INSTANCE.fromSshServerConfig(sshServerConfig);
        SshServerConfig sshServerConfigOne = lazyLambdaStream.selectOne(
                LazyWrappers.<SshServerConfigDO>lambdaWrapper()
                        .eqIgnoreEmpty(SshServerConfigDO::getCreateTime, sshServerConfigDO.getCreateTime())
                        .eqIgnoreEmpty(SshServerConfigDO::getHost, sshServerConfigDO.getHost())
                        .eqIgnoreEmpty(SshServerConfigDO::getId, sshServerConfigDO.getId())
                        .eqIgnoreEmpty(SshServerConfigDO::getIsDeleted, sshServerConfigDO.getIsDeleted())
                        .eqIgnoreEmpty(SshServerConfigDO::getName, sshServerConfigDO.getName())
                        .eqIgnoreEmpty(SshServerConfigDO::getPassword, sshServerConfigDO.getPassword())
                        .eqIgnoreEmpty(SshServerConfigDO::getPort, sshServerConfigDO.getPort())
                        .eqIgnoreEmpty(SshServerConfigDO::getUpdateTime, sshServerConfigDO.getUpdateTime())
                        .eqIgnoreEmpty(SshServerConfigDO::getUsername, sshServerConfigDO.getUsername())
                        .orderByDesc(SshServerConfigDO::getCreateTime)
                , SshServerConfig.class);
        return ResultFactory.successOf(sshServerConfigOne);
    }

    /**
     * describe 查询多个ssh服务器信息
     *
     * @param sshServerConfig 查询多个ssh服务器信息
     * @return {@link Result<List<SshServerConfig>>} ssh服务器信息领域对象
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Override
    public Result<List<SshServerConfig>> findList(SshServerConfig sshServerConfig) {
        SshServerConfigDO sshServerConfigDO = SshServerConfigConverter.INSTANCE.fromSshServerConfig(sshServerConfig);
        List<SshServerConfig> sshServerConfigList = lazyLambdaStream.selectList(
                LazyWrappers.<SshServerConfigDO>lambdaWrapper()
                        .eqIgnoreEmpty(SshServerConfigDO::getCreateTime, sshServerConfigDO.getCreateTime())
                        .eqIgnoreEmpty(SshServerConfigDO::getHost, sshServerConfigDO.getHost())
                        .eqIgnoreEmpty(SshServerConfigDO::getId, sshServerConfigDO.getId())
                        .eqIgnoreEmpty(SshServerConfigDO::getIsDeleted, sshServerConfigDO.getIsDeleted())
                        .eqIgnoreEmpty(SshServerConfigDO::getName, sshServerConfigDO.getName())
                        .eqIgnoreEmpty(SshServerConfigDO::getPassword, sshServerConfigDO.getPassword())
                        .eqIgnoreEmpty(SshServerConfigDO::getPort, sshServerConfigDO.getPort())
                        .eqIgnoreEmpty(SshServerConfigDO::getUpdateTime, sshServerConfigDO.getUpdateTime())
                        .eqIgnoreEmpty(SshServerConfigDO::getUsername, sshServerConfigDO.getUsername())
                        .orderByDesc(SshServerConfigDO::getCreateTime)
                , SshServerConfig.class);
        return ResultFactory.successOf(sshServerConfigList);
    }

    /**
     * describe 分页查询多个ssh服务器信息
     *
     * @param size            当前页数
     * @param current         当前页
     * @param sshServerConfig 分页查询多个ssh服务器信息
     * @return {@link Result<LazyPage<SshServerConfig>>} 分页ssh服务器信息领域对象
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Override
    public Result<LazyPage<SshServerConfig>> findPage(int size, int current, SshServerConfig sshServerConfig) {
        SshServerConfigDO sshServerConfigDO = SshServerConfigConverter.INSTANCE.fromSshServerConfig(sshServerConfig);
        LazyPage<SshServerConfig> lazyPage = new LazyPage<>(current, size);
        LazyPage<SshServerConfig> sshServerConfigLazyPage = lazyLambdaStream.selectPage(
                LazyWrappers.<SshServerConfigDO>lambdaWrapper()
                        .eqIgnoreEmpty(SshServerConfigDO::getCreateTime, sshServerConfigDO.getCreateTime())
                        .eqIgnoreEmpty(SshServerConfigDO::getHost, sshServerConfigDO.getHost())
                        .eqIgnoreEmpty(SshServerConfigDO::getId, sshServerConfigDO.getId())
                        .eqIgnoreEmpty(SshServerConfigDO::getIsDeleted, sshServerConfigDO.getIsDeleted())
                        .eqIgnoreEmpty(SshServerConfigDO::getName, sshServerConfigDO.getName())
                        .eqIgnoreEmpty(SshServerConfigDO::getPassword, sshServerConfigDO.getPassword())
                        .eqIgnoreEmpty(SshServerConfigDO::getPort, sshServerConfigDO.getPort())
                        .eqIgnoreEmpty(SshServerConfigDO::getUpdateTime, sshServerConfigDO.getUpdateTime())
                        .eqIgnoreEmpty(SshServerConfigDO::getUsername, sshServerConfigDO.getUsername())
                        .orderByDesc(SshServerConfigDO::getCreateTime)
                , lazyPage, SshServerConfig.class);
        return ResultFactory.successOf(sshServerConfigLazyPage);
    }

    /**
     * describe 删除ssh服务器信息
     *
     * @param sshServerConfig 删除ssh服务器信息
     * @return {@link Result<SshServerConfig>} ssh服务器信息
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Override
    public Result<SshServerConfig> remove(SshServerConfig sshServerConfig) {
        SshServerConfigDO sshServerConfigDO = SshServerConfigConverter.INSTANCE.fromSshServerConfig(sshServerConfig);
        lazyLambdaStream.delete(
                LazyWrappers.<SshServerConfigDO>lambdaWrapper()
                        .eqIgnoreEmpty(SshServerConfigDO::getCreateTime, sshServerConfigDO.getCreateTime())
                        .eqIgnoreEmpty(SshServerConfigDO::getHost, sshServerConfigDO.getHost())
                        .eqIgnoreEmpty(SshServerConfigDO::getId, sshServerConfigDO.getId())
                        .eqIgnoreEmpty(SshServerConfigDO::getIsDeleted, sshServerConfigDO.getIsDeleted())
                        .eqIgnoreEmpty(SshServerConfigDO::getName, sshServerConfigDO.getName())
                        .eqIgnoreEmpty(SshServerConfigDO::getPassword, sshServerConfigDO.getPassword())
                        .eqIgnoreEmpty(SshServerConfigDO::getPort, sshServerConfigDO.getPort())
                        .eqIgnoreEmpty(SshServerConfigDO::getUpdateTime, sshServerConfigDO.getUpdateTime())
                        .eqIgnoreEmpty(SshServerConfigDO::getUsername, sshServerConfigDO.getUsername())
        );
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在ssh服务器信息
     *
     * @param sshServerConfig ssh服务器信息领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Override
    public Result<Boolean> exists(SshServerConfig sshServerConfig) {
        SshServerConfigDO sshServerConfigDO = SshServerConfigConverter.INSTANCE.fromSshServerConfig(sshServerConfig);
        Boolean exists = lazyLambdaStream.exists(
                LazyWrappers.<SshServerConfigDO>lambdaWrapper()
                        .eqIgnoreEmpty(SshServerConfigDO::getCreateTime, sshServerConfigDO.getCreateTime())
                        .eqIgnoreEmpty(SshServerConfigDO::getHost, sshServerConfigDO.getHost())
                        .eqIgnoreEmpty(SshServerConfigDO::getId, sshServerConfigDO.getId())
                        .eqIgnoreEmpty(SshServerConfigDO::getIsDeleted, sshServerConfigDO.getIsDeleted())
                        .eqIgnoreEmpty(SshServerConfigDO::getName, sshServerConfigDO.getName())
                        .eqIgnoreEmpty(SshServerConfigDO::getPassword, sshServerConfigDO.getPassword())
                        .eqIgnoreEmpty(SshServerConfigDO::getPort, sshServerConfigDO.getPort())
                        .eqIgnoreEmpty(SshServerConfigDO::getUpdateTime, sshServerConfigDO.getUpdateTime())
                        .eqIgnoreEmpty(SshServerConfigDO::getUsername, sshServerConfigDO.getUsername())
        );
        return ResultFactory.successOf(exists);
    }

}